package com.fivetalks.controller.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fivetalks.domain.Member;
import com.fivetalks.service.MemberService;

@Controller
public class MemberController {
	// /member
	@Autowired
	MemberService memberService;

	@RequestMapping(value = "/member/join", method = RequestMethod.POST)
	public String join(@ModelAttribute Member member, Model model) {
		// 이메일 중복
		System.out.println(member.getGender());
		if (memberService.emailExists(member.getEmail()) == true) {
			model.addAttribute("flag", "duplicated");
			return "/member/join";
		} else {
			memberService.saveMember(member);
			model.addAttribute("flag", "ok");
			return "/member/join";
		}
	}

	@RequestMapping(value = "/member/login", method = RequestMethod.POST)
	public String login(@ModelAttribute Member member, Model model) {
		// 이메일 검사,패스워드 검사

		if (memberService.loginExists(member.getEmail(), member.getPassword()) == true) {
			model.addAttribute("flag", "ok");
			return "/member/login";
		} else {
			model.addAttribute("flag", "fail");
			return "/member/login";
		}
	}

	@RequestMapping(value = "/member/getinfo", method = RequestMethod.POST)
	public String getinfo(@ModelAttribute Member member, Model model) {
		// 이메일 검사,패스워드 검사

		if (memberService.loginExists(member.getEmail(), member.getPassword()) == true) {
			model.addAttribute("flag", "ok");
			Member temp;

			for (int i = 0; i < memberService.getMemberList().size(); i++) {
				temp = memberService.getMemberList().get(i);
				if (member.getEmail().equals(temp.getEmail())) {
					model.addAttribute("id", temp.getId());
					model.addAttribute("email", temp.getEmail());
					model.addAttribute("password", temp.getPassword());
					model.addAttribute("nickname", temp.getNickname());
					model.addAttribute("age", temp.getAge());
					model.addAttribute("gender", temp.getGender());
				}
			}
			return "/member/getinfo";
		} else {
			System.out.println("hhhhh");
			model.addAttribute("flag", "fail");
			return "/member/getinfo";
		}

	}
}
